﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WIKID.Model;
using System.Xml;
using WIKID.Common;
namespace WIKID.Accessor
{
    class XMLReader
    {
        

        // read file xml to get account model by filename
        public AccountModel getAccountByXmlFileName(String fileName)
        {
            Utils utils = new Utils();
            List<string> lsFiles = utils.ListingXMLFiles(Constants.ACCOUNTS_FOLDER);
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(Constants.ACCOUNTS_FOLDER + @"\" + fileName + ".xml");
            }
            catch
            {
                throw new Exception("File not found!");
            }
            
            AccountModel accountModel = new AccountModel();
            accountModel.AccId = doc.SelectSingleNode("//@accId").Value;
            accountModel.AccName = fileName.Substring(fileName.IndexOf("_")+1);
            accountModel.AccPic = doc.SelectSingleNode("//account/accPic").InnerText;
            try
            {
                accountModel.AccAge = Convert.ToInt32(doc.SelectSingleNode("//account/accAge").InnerText);
            }
            catch (Exception ex)
            {
                //throw ex;
            }
            accountModel.PassId = doc.SelectSingleNode("//account/passId").InnerText;
            accountModel.PassPic = doc.SelectSingleNode("//account/passPic").InnerText;
            XmlNodeList scoreNodeList = doc.GetElementsByTagName("score");
            if (scoreNodeList.Count == 0) accountModel.Scores = new List<ScoreModel>();
            else
            {

                accountModel.Scores = new List<ScoreModel>();
                foreach (XmlNode node in scoreNodeList)
                {
                    ScoreModel scoreModel = new ScoreModel();

                    scoreModel.LectureId = node.ChildNodes[0].InnerText;
                    scoreModel.StudyingScore = decimal.Parse(node.ChildNodes[1].InnerText);
                    scoreModel.AttitudeScore = decimal.Parse(node.ChildNodes[2].InnerText);
                    scoreModel.Date = DateTime.Parse(node.ChildNodes[3].InnerText);
                    accountModel.Scores.Add(scoreModel);

                }
            }
            
            XmlNodeList scheduleNodeList = doc.GetElementsByTagName("schedule");
            if (scheduleNodeList.Count == 0) accountModel.Schedules = new List<ScheduleModel>();
            else 
            {
                accountModel.Schedules = new List<ScheduleModel>();
                foreach (XmlNode node in scheduleNodeList) 
                {
                    ScheduleModel scheduleModel = new ScheduleModel();
                    scheduleModel.LectureId = node.ChildNodes[0].InnerText;
                    scheduleModel.StartDateTime = DateTime.Parse(node.ChildNodes[1].InnerText);
                    scheduleModel.EndDateTime = DateTime.Parse(node.ChildNodes[2].InnerText);
                    accountModel.Schedules.Add(scheduleModel);
                }
            }
            return accountModel;
        }

        public List<QuestionModel> getQuestionsByXmlFileName(String questionFileAbsolutePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(questionFileAbsolutePath);
            List<QuestionModel> questionModels = new List<QuestionModel>();
            XmlNodeList questionNodeList = doc.GetElementsByTagName("question");

            foreach (XmlNode node in questionNodeList)
            {
                QuestionModel questionModel = new QuestionModel();
                questionModel.ID = node.Attributes[0].Value;
                questionModel.ImagePath = node.ChildNodes[0].InnerText;
                questionModel.Description = node.ChildNodes[1].InnerText;
                //Create a new List of Answer 
                questionModel.Answers = new List<AnswerModel>();
                //Select all Answer follow by questionID
                XmlNodeList answerNodeList = doc.SelectNodes("content/question[@id='" + questionModel.ID + "']/answer");
                
                foreach (XmlNode xmlNode in answerNodeList)
                {
                    AnswerModel answer = new AnswerModel();
                    answer.ID = xmlNode.Attributes[0].Value;
                    answer.Content = xmlNode.InnerText;
                    questionModel.Answers.Add(answer);
                }
                questionModel.Solution = node.ChildNodes[questionModel.Answers.Count + 2].InnerText;
                questionModels.Add(questionModel);
            }

            return questionModels;
        }

        public List<LectureModel> getLecturesByXmlFileName(String lectureFileAbsolutePath)
        {
            List<LectureModel> lectureModels = new List<LectureModel>();
            XmlDocument doc = new XmlDocument();
            doc.Load(lectureFileAbsolutePath);
            XmlNodeList lectureNodeList = doc.GetElementsByTagName("lecture");
            foreach (XmlNode node in lectureNodeList)
            {
                LectureModel lectureModel = new LectureModel();
                lectureModel.LectureId = node.Attributes[0].Value;
                lectureModel.LectureTitle = node.ChildNodes[0].InnerText;
                lectureModel.LectureQuestion = node.ChildNodes[1].InnerText;
                lectureModels.Add(lectureModel);
            }
            return lectureModels;
        }
        
    }
}
